package com.whfc.ms.api.safety.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.safety.service.MsSafetyPartService;
import com.whfc.safety.dto.SafetyPartDTO;
import com.whfc.safety.dto.SafetyPartImportResultDTO;
import com.whfc.safety.param.SafetyPartAddParam;
import com.whfc.safety.param.SafetyPartEditParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 安全部位
 *
 * @author : qzexing
 * @version : 1.0
 * @date : 2020-07-30 15:38
 */
@RequiresPermissions("safety/check")
@RestController
@RequestMapping("/ms/api/safety/part")
public class SafetyPartController extends BaseController {

    @Autowired
    private MsSafetyPartService msSafetyPartService;

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("安全检查部位列表,loginer:{},deptId:{},keyword:{}", user.getUsername(), deptId, keyword);
        List<SafetyPartDTO> list = msSafetyPartService.list(deptId, keyword);
        return ResultUtil.success(new ListData<>(list));
    }

    @RequiresPermissions("safety/check/add")
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @RequestBody @Validated SafetyPartAddParam request) {
        logger.info("新增安全检查部位,loginer:{},deptId:{},pid:{},code:{},name:{}", user.getUsername(), request.getDeptId(),
                request.getPid(), request.getCode(), request.getName());
        msSafetyPartService.add(request);
        return ResultUtil.success();
    }

    @RequiresPermissions("safety/check/edit")
    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestBody @Validated SafetyPartEditParam request) {
        logger.info("编辑安全检查部位,loginer:{},partId:{},code:{},name:{}", user.getUsername(),
                request.getPartId(), request.getCode(), request.getName());
        msSafetyPartService.edit(request);
        return ResultUtil.success();
    }

    @RequiresPermissions("safety/check/delete")
    @PostMapping("/del/{partId}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @PathVariable(value = "partId") Integer partId) {
        logger.info("删除安全检查部位,loginer:{},partId:{}", user.getUsername(), partId);
        msSafetyPartService.del(partId);
        return ResultUtil.success();
    }

    @RequiresPermissions("safety/check/exportAll")
    @PostMapping("/importExcel")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @RequestParam(value = "deptId") Integer deptId,
                      @RequestParam MultipartFile file) {
        logger.info("导入安全检查部位,loginer:{},deptId:{},fileName:{}", user.getUsername(), deptId, file.getOriginalFilename());
        SafetyPartImportResultDTO resultDTO = msSafetyPartService.importExcel(deptId, file);
        return ResultUtil.success(resultDTO);
    }

}
